package com.weakteam.orderonline.webclient.web;

import com.weakteam.orderonline.dataresource.domain.Goods;
import com.weakteam.orderonline.dataresource.domain.Menu;
import com.weakteam.orderonline.dataresource.service.GoodsService;
import com.weakteam.orderonline.dataresource.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * @author Nail
 */
@Controller
public class DataNetService {

    @Autowired
    MenuService menuService;

    @Autowired
    GoodsService goodsService;

    private static final Logger log = LoggerFactory.getLogger(DataNetService.class);

    private static void initJSONHeader(final HttpServletResponse httpResponse){
        httpResponse.setHeader("Connection", "close");
        httpResponse.setHeader("Content-Type", "application/json");
        httpResponse.setHeader("Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
        httpResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        httpResponse.setHeader("Pragma", "no-cache");
        httpResponse.setCharacterEncoding("UTF-8");
    }

    // TODO: add cash for this request
    @RequestMapping(value = "/getPriceData")
    public void getPriceData(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        try {
            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            List<Menu> menus = menuService.getAll();
            out.print("[");
            for (Iterator<Menu> itMenu = menus.iterator(); itMenu.hasNext();) {
                Menu menu = itMenu.next();
                List<Goods> goodsList = goodsService.getAllEnabledByMenuId(menu.getId());

                out.print("{\"id\":"); out.print(menu.getId()); out.print(",");
                out.print("\"name\":\""); out.print(menu.getName()); out.print("\",");
                out.print("\"description\":\""); out.print(menu.getDescription()); out.print("\",");
                out.print("\"ico\":\""); out.print(menu.getIco()); out.print("\",");
                out.print("\"image\":\""); out.print(menu.getImage()); out.print("\",");
                out.print("\"properties\":\""); out.print(menu.getProperties()); out.print("\",");
                out.print("\"goods\":[");
                for (Iterator<Goods> itGoods = goodsList.iterator(); itGoods.hasNext();) {
                    Goods goods = itGoods.next();
                    out.print("{\"id\":"); out.print(goods.getId()); out.print(",");
                    out.print("\"price\":"); out.print(goods.getPrice()); out.print(",");
                    out.print("\"name\":\""); out.print(goods.getName()); out.print("\",");
                    out.print("\"description\":\""); out.print(goods.getDescription()); out.print("\",");
                    out.print("\"ico\":\""); out.print(goods.getIco()); out.print("\",");
                    out.print("\"image\":\""); out.print(goods.getImage()); out.print("\",");
                    out.print("\"properties\":\""); out.print(goods.getProperties()); out.print("\"}");
                    if (itGoods.hasNext()) out.println(",");
                }
                out.println("]}");
                if (itMenu.hasNext()) out.println(",");
            }
            out.print("]");
            out.flush();
            out.close();
        } catch (IOException ex) {
            log.error("getPriceData {}", ex);
        }
    }
}
